﻿// Groupon.NET C# Library
// Copyright (c) 2011 James Paul Duncan (JPaulDuncan)
// The MIT License (MIT)
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// http://www.jpaulduncan.com

using System;

namespace Groupon.Net
{
    public static class Helpers
    {
        public static bool? ToBool(this TriState value)
        {
            switch (value)
            {
                case TriState.True: return true;
                case TriState.False: return false;
                default: return null;
            }
        }
    }

    internal static class Extenders
    {

        public static bool IsReady(this System.Xml.XmlNode item)
        {
            return (item != null && (item.InnerText.HasValue()));
        }

        public static bool HasValue(this string value)
        {
            if (value != null && value.Length > 0)
            {
                return true;
            }
            return false;
        }

        public static string GetText(this System.Xml.XmlNode item)
        {
            if (item != null)
            {
                return item.InnerText;
            }
            return string.Empty;
        }

        public static bool GetBool(this System.Xml.XmlNode item)
        {
            if (item.IsReady())
            {
                return item.InnerText == "true" ? true : false;
            }
            return false;
        }

        public static DateTime GetDateTime(this System.Xml.XmlNode item)
        {
            if (item.IsReady())
            {
                return Convert.ToDateTime(item.InnerText);
            }
            return DateTime.MinValue;
        }

        public static int GetInt(this System.Xml.XmlNode item)
        {
            if (item.IsReady())
            {
                return Convert.ToInt32(item.InnerText);

            }
            return 0;
        }

        public static TriState GetTriState(this System.Xml.XmlNode item)
        {
            if (item.IsReady())
            {
                return item.InnerText == "true" ? TriState.True : TriState.False;
            }
            return TriState.Unknown;
        }
    }
}
